<template>
  <div class="info-container">
    <el-card style="height: 100%">
      <el-button @click="handleAdd" type="primary" style="margin-bottom: 24px"
        >添加</el-button
      >
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        height="84%"
      >
        <el-table-column label="序号" type="index" width="80" />
        <el-table-column prop="pipeName" label="所属管廊"> </el-table-column>
        <el-table-column prop="dateTime" label="日期"> </el-table-column>
        <el-table-column prop="indexNumber" label="本期指数"> </el-table-column>
        <el-table-column prop="lastIndexNumber" label="上期指数">
        </el-table-column>

        <el-table-column
          prop="chargeableElectricity"
          label="计费电量"
          width="150"
        >
        </el-table-column>
        <el-table-column prop="amount" label="金额" width="150">
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="260">
          <template #default="scope">
            <div class="action-row">
              <svg-icon icon="edit" @click.prevent="handleEdit(scope.row)" />
              <div class="has-more"></div>
              <svg-icon icon="dele" @click="handleDel(scope.row)" />
            </div>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 20]"
        :page-size="size"
        layout="prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <add-dialog
      :title="dialogTitle"
      :rowData="curRowData"
      v-model:visible="visibleDialog"
      @onReload="reloadPage"
      ref="dialogClear"
    ></add-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import AddDialog from './components/AddDialog/index.vue'
import { ElMessageBox } from 'element-plus'
import { energyList, energyDel } from '@/api/back'
import { message } from '@/utils/message'
//数据相关
const dialogClear = ref(null)
const tableData = ref([])
const total = ref(0)
const page = ref(1)
const size = ref(8)
const params = ref({})
const loading = ref(false)
const visibleDialog = ref(false)
const curRowData = ref({})
const dialogTitle = ref('')

// 获取数据的方法
const getListData = async (params) => {
  loading.value = true
  const result = await energyList(params, page.value, size.value)
  loading.value = false
  tableData.value = result.rows
  total.value = result.total
}

const reloadPage = () => {
  getListData(params.value)
  if (visibleDialog.value) {
    visibleDialog.value = false
  }
}

reloadPage()
//查询条件相关
const handleSearch = (paramsData) => {
  params.value = paramsData
  getListData(paramsData)
}
// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getListData(params.value)
}

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getListData(params.value)
}

const handleAdd = () => {
  dialogTitle.value = '新增能耗'
  curRowData.value = {}
  visibleDialog.value = true
  dialogClear.value.cleara()
}
const handleEdit = (row) => {
  dialogTitle.value = '编辑能耗'
  curRowData.value = row
  visibleDialog.value = true
  dialogClear.value.cleara()
}
// 删除信息
const handleDel = (row) => {
  ElMessageBox.confirm('确定删除此能耗管理?', '删除能耗管理', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  })
    .then(() => {
      // 删除
      deleList(row.id)
    })
    .catch(() => {})
}
const deleList = async (id) => {
  const res = await energyDel(id)
  message({ type: 'success', message: '删除成功!' })
  getListData(params.value)
}
</script>

<style lang="scss" scoped>
.info-container {
  width: 100%;
  height: 100%;

  .action-row {
    display: flex;
    .has-more {
      width: 2px;
      height: 10px;
      margin-right: 10px;
      margin-left: 10px;
      margin-top: 5px;
    }
  }
  .pagination {
    width: 100%;
    height: 32px;
    margin-top: 20px;
    display: flex;
    align-items: center;
    justify-content: end;
  }
}
</style>
